package com.example.ssedemo.web;

import com.example.ssedemo.bean.Message;
import com.example.ssedemo.service.SseEmitterManager;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

@Slf4j
@RestController
@RequestMapping("/sse")
public class SseController {

    @Resource
    private SseEmitterManager sseEmitterManager;

    @CrossOrigin("*")
    @GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter index(String id) {
        return sseEmitterManager.connect(id);
    }

    @GetMapping("send")
    public String index2(@RequestBody Message message) {
        sseEmitterManager.publish(message);
        return "发送消息";
    }
}
